package think.thread.sychron.c02;

import java.util.concurrent.TimeUnit;

public class Chef implements Runnable{
	
	private Restaurant restaurant;
	int count=0;
	
	public Chef(Restaurant restaurant){
		this.restaurant=restaurant;
	}

	public void run() {
		try {
			
			while (!Thread.interrupted()) {
				synchronized (this) {
					while(restaurant.meal!=null){
						wait();
					}
				}
				
				if(++count==10){
					System.out.println("out of food,closing");
					restaurant.exec.shutdownNow();
				}
				
				synchronized (restaurant.waitPerson) {
					restaurant.meal=new Meal(count);
					System.out.println("order "+count);
					restaurant.waitPerson.notifyAll();
				}
				TimeUnit.MILLISECONDS.sleep(100);
			}
			
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println("Chef interrupted");
		}
	}
	
}
